<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="客户名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="departName" :rules="[{required: true, message: '请输入客户名称!'}]">
              <a-input v-model.trim="model.departName" placeholder="请输入客户名称(最多30字)" :maxLength="30" :disabled="title === '编辑客户'"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="手机号码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mobile">
              <a-input v-model.trim="model.mobile" placeholder="请输入手机号码"></a-input>
            </a-form-model-item>
          </a-col>
          <template v-if="title === '新增'">
            <a-col :span="24">
              <a-form-model-item label="登录密码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="initPassword" has-feedback>
                <a-input type="password" placeholder="请输入登录密码" v-model.trim="model.initPassword" />
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="确认密码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="confirmpassword" has-feedback>
                <a-input type="password" placeholder="请重新输入登录密码" v-model.trim="model.confirmpassword" />
              </a-form-model-item>
            </a-col>
          </template>
          <a-col :span="24">
            <a-form-model-item label="是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-switch v-model="model.status" ></j-switch>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="创建类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createType">
              <j-dict-select-tag type="list" v-model="model.createType" dictCode="merchant_create_type" placeholder="请选择创建类型" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="客户级别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="departLevel">
              <j-dict-select-tag type="list" v-model="model.departLevel" dictCode="merchant_level" placeholder="请选择客户级别" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="客户类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="merchantType">
              <j-dict-select-tag type="list" v-model="model.merchantType" dictCode="merchant_type" placeholder="请选择客户类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="分组设置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="groupId">
              <j-dict-select-tag type="list" v-model="model.groupId" dictCode="hz_sys_merchant_group,name,id,system" placeholder="请选择分组设置" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="组织权限" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="maxPermRoleId">
              <j-multi-select-tag  v-model="model.maxPermRoleId" :options="rolesOptions" placeholder="请选择组织权限"></j-multi-select-tag>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { postAction, putAction } from '@/api/manage'
export default {
  name: "SysMerchantOrgForm",
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    },
    title: {
      type: String,
      default: ''
    },
    rolesOptions: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      model:{
        status: 0
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        mobile: [{required: true, pattern: /^1[3-9][0-9]{9}$/, message: "请输入正确的手机号码!"}],
        initPassword: [
          {
            required: true,
            pattern:/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9a-zA-Z]{8,16}$/,
            message: '密码由8-16位数字、大小写字母组成!'
          },
          {validator: this.validateToNextPassword}
        ],
        confirmpassword: [
          {required: true, message: '请重新输入登录密码!'},
          {validator: this.compareToFirstPassword}
        ],
        departLevel: [{required: true, message: "请选择客户级别!"}],
        merchantType:[{required: true, message: "请选择客户类型!"}],
        maxPermRoleId: [{required: true, message: "请选择组织权限!"}]
      },
      url: {
        add: "/sys/sysDepart/add",
        edit: "/sys/sysDepart/edit",
        queryById: "/sys/sysDepart/queryById"
      }
    }
  },
  computed: {
    formDisabled(){
      return this.disabled
    },
  },
  created () {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    add () {
      this.edit(this.modelDefault);
    },
    edit (record) {
      this.model = Object.assign({}, record, {status: record.status === '1' ? 'Y' : 'N'});
      this.visible = true;
    },
    submitForm () {
      const that = this;
      // 触发表单验证
      that.$refs.form.validate(valid => {
        if (valid) {
          let httpurl = '';
          let Action;
          let params = Object.assign({}, that.model, {status: that.model.status === 'Y' ? '1' : '0'});
          params["orgCategory"] ="1";  //默认为公司创建
          if(!that.model.id){
            Action = postAction;
            httpurl+=that.url.add;
            //代表2级组织,默认组织兰谷下边的
            //params.orgType = '2';
          }else{
            Action = putAction;
            httpurl+=that.url.edit;
          }
          that.confirmLoading = true;
          Action(httpurl,params).then((res)=>{
            if(res.success){
              that.$message.success(res.message);
              that.$emit('ok');
            }else{
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }
      })
    },
    validateToNextPassword  (rule, value, callback) {
      if (value && this.model.confirmpassword && value !== this.model.confirmpassword) {
        callback('两次输入的密码不一样！');
      }else{
        callback();
      }
    },
    compareToFirstPassword  (rule, value, callback) {
      if (value && this.model.initPassword && value !== this.model.initPassword) {
        callback('两次输入的密码不一样！');
      } else {
        callback()
      }
    }
  }
}
</script>